package pers.cz.business;

import cn.hutool.core.date.DateUtil;

import pers.cz.mapper.*;
import pers.cz.model.*;
import pers.cz.mybatisPlugin.PageRowBounds;
import pers.cz.pub.AbstractBusiness;
import pers.cz.pub.SequenceEnum;

import java.util.List;

/**
 * @program: PostGirl-panent
 * @description: AutoTestCaseBusiness
 * @author: Cheng Zhi
 * @create: 2021-11-10 19:07
 **/
public class AutoTestCaseBusiness extends AbstractBusiness {

    /**
     * 分页查询所有测试案例
     * @param pageStart
     * @param pageLimit
     * @return
     */
    public List<AutoTestCase> queryAllAutoTestCaseByPage(Integer pageStart, Integer pageLimit) {

        PageRowBounds pageRowBounds = new PageRowBounds(pageStart, pageLimit);
        return this.getMapper(AutoTestCastMapper.class).selectAllAutoTestCaseByPage(pageRowBounds);
    }

    /**
     * 查询所有的测试案例
     * @return
     */
    public List<AutoTestCase> queryAllAutoTestCase() {

        return this.getMapper(AutoTestCastMapper.class).selectAllAutoTestCase();
    }
    /**
     * 获取总数
     * @return
     */
    public Integer queryCount() {

        return this.getMapper(AutoTestCastMapper.class).selectCount();
    }
    /**
     * 模糊查询测试案例
     * @param testCaseName
     * @return
     */
    public List<AutoTestCase> selectAutoTestCaseByFuzzy(String testCaseName) {
        AutoTestCastExample example = new AutoTestCastExample();
        AutoTestCastExample.Criteria criteria = example.createCriteria();
        criteria.andCaseNameLike("%" + testCaseName + "%");
        return this.getMapper(AutoTestCastMapper.class).selectByExample(example);
    }

    /**
     * 添加测试案例
     * @param autoTestCase
     * @return
     */
    public Integer addAutoTestCase(AutoTestCase autoTestCase) {

        return this.getMapper(AutoTestCastMapper.class).insert(autoTestCase);
    }

    /**
     * 获取一个测试案例的Id，用于后序插入
     * @return
     */
    public Long getAutoTestCaseId() {

        return this.getcPublicFunc().getSoNbr(SequenceEnum.AUTO_TESTCASE);
    }

    /**
     * 添加案例对应的参数
     * @param autoTestCaseParams
     * @return
     */
    public Integer addAutoTestCaseParams(AutoTestCaseParams autoTestCaseParams, AutoTestCaseParamsSql autoTestCaseParamsSql, AutoTestCaseParamsCode autoTestCaseParamsCode, AutoTestCaseParamsRegular autoTestCaseParamsRegular) {
        Long id = this.getcPublicFunc().getSoNbr(SequenceEnum.TESTCASE_PARAMS_SECQUENCE);
        if (autoTestCaseParamsCode != null) {
            addAutoTestCaseParamsCode(autoTestCaseParamsCode, Integer.valueOf(id + ""));
        } else if (autoTestCaseParamsSql != null) {
            addAutoTestCaseParamsSql(autoTestCaseParamsSql, Integer.valueOf(id + ""));
        } else if (autoTestCaseParamsRegular != null) {
            addAutoTestCaseParamsRegular(autoTestCaseParamsRegular, Integer.valueOf(id + ""));
        }
        autoTestCaseParams.setId(Integer.valueOf(id + ""));
        return this.getMapper(AutoTestCaseParamsMapper.class).insert(autoTestCaseParams);
    }

    public Integer addAutoTestCaseParamsRegular(AutoTestCaseParamsRegular autoTestCaseParamsRegular, Integer id) {
        autoTestCaseParamsRegular.setParamId(id);
        return this.getMapper(AutoTestCaseParamsRegularMapper.class).insertSelective(autoTestCaseParamsRegular);
    }

    public Integer addAutoTestCaseParamsSql(AutoTestCaseParamsSql autoTestCaseParamsSql, Integer id) {
        autoTestCaseParamsSql.setParamId(id);
        return this.getMapper(AutoTestCaseParamsSqlMapper.class).insertSelective(autoTestCaseParamsSql);
    }

    public Integer addAutoTestCaseParamsCode(AutoTestCaseParamsCode autoTestCaseParamsCode, Integer id) {
        autoTestCaseParamsCode.setParamId(id);
        return this.getMapper(AutoTestCaseParamsCodeMapper.class).insertSelective(autoTestCaseParamsCode);
    }

    /**
     * 添加案例对应的检查点
     * @param autoTestCaseCheck
     * @return
     */
    public Integer addAutoTestCaseChecks(AutoTestCaseCheck autoTestCaseCheck) {
        Long id = this.getcPublicFunc().getSoNbr(SequenceEnum.TESTCASE_CHECK_SECQUENCE);
        autoTestCaseCheck.setId(Integer.valueOf(id + ""));
        return this.getMapper(AutoTestCaseCheckMapper.class).insert(autoTestCaseCheck);
    }

    /**
     * 添加公共入参参数
     * @param autoPublicParams
     * @return
     */
    public Integer addAutoPublicParams(AutoPublicParams autoPublicParams) {
        Long id = this.getcPublicFunc().getSoNbr(SequenceEnum.PUBLIC_PARAMS_SECQUENCE);
        autoPublicParams.setId(Integer.valueOf(id + ""));
        autoPublicParams.setUpdatedTime(DateUtil.date());
        return this.getMapper(AutoPublicParamsMapper.class).insert(autoPublicParams);
    }

    /**
     * 删除测试案例，以及和测试案例相关的内容
     * @param id 测试案例id
     * @return
     */
    public Integer deleteAutoTestCase(Integer id) {
        int count = this.getMapper(AutoTestCastMapper.class).deleteByPrimaryKey(id);
        // 删除参数和检查点还有公共参数
        this.getMapper(AutoTestCaseCheckMapper.class).deleteByTestCaseId(id);
        // this.getMapper(AutoTestCaseParamsMapper.class).deleteByTestCaseId(id);
        this.getMapper(AutoPublicParamsMapper.class).deleteByTestCaseId(id); // 删除参数的同时还删除了代码脚本和sql脚本以及关联sql、公共参数

        return count;
    }

    /**
     * 批量删除测试案例
     * @param ids
     * @return
     */
    public Integer deleteAutoTestCaseBatch(List<Integer> ids) {

        int count = this.getMapper(AutoTestCastMapper.class).batchDelete(ids);
        // 删除参数和检查点还有公共参数
        this.getMapper(AutoTestCaseCheckMapper.class).batchDelete(ids);
        this.getMapper(AutoTestCaseParamsMapper.class).batchDelete(ids);
        this.getMapper(AutoPublicParamsMapper.class).batchDelete(ids);
        return count;
    }

    /**
     * 查询检查点
     * @param testCaseId
     * @return
     */
    public List<AutoTestCaseCheck> queryAutoTestCaseCheckByTestCaseId(Integer testCaseId) {

        return this.getMapper(AutoTestCaseCheckMapper.class).selectByTestCaseId(testCaseId);
    }

    /**
     * 修改检查点
     * @param autoTestCaseCheck
     * @return
     */
    public Integer updateAutoTestCaseCheck(AutoTestCaseCheck autoTestCaseCheck) {

        AutoTestCaseCheckExample autoTestCaseCheckExample = new AutoTestCaseCheckExample();
        AutoTestCaseCheckExample.Criteria criteria = autoTestCaseCheckExample.createCriteria();
        criteria.andIdEqualTo(autoTestCaseCheck.getId());

        return this.getMapper(AutoTestCaseCheckMapper.class).updateByExampleSelective(autoTestCaseCheck,autoTestCaseCheckExample);
    }

    /**
     * 修改入参时回显
     * @param testCaseId
     * @return
     */
    public List<AutoTestCaseParams> queryAutoTestCaseParams(Integer testCaseId) {

        return this.getMapper(AutoTestCaseParamsMapper.class).selectByTestCaseId(testCaseId);
    }

    /**
     * 修改自定义入参
     * @param autoTestCaseParams
     * @return
     */
    public Integer updateAutoTestCaseParams(AutoTestCaseParams autoTestCaseParams) {
        return this.getMapper(AutoTestCaseParamsMapper.class).updateByPrimaryKeySelective(autoTestCaseParams);
    }

    /**
     * 修改公共入参时回显
     * @param testCaseId
     * @return
     */
    public List<AutoPublicParams> queryAutoPublicParams(Integer testCaseId) {

        return this.getMapper(AutoPublicParamsMapper.class).selectByTestCaseId(testCaseId);
    }

    /**
     * 修改公共入参
     * @param autoPublicParams
     * @return
     */
    public Integer updateAutoPublicParams(AutoPublicParams autoPublicParams) {
        AutoPublicParamsExample autoTestCaseCheckExample = new AutoPublicParamsExample();
        AutoPublicParamsExample.Criteria criteria = autoTestCaseCheckExample.createCriteria();
        criteria.andIdEqualTo(autoPublicParams.getId());
        return this.getMapper(AutoPublicParamsMapper.class).updateByExampleSelective(autoPublicParams, autoTestCaseCheckExample);
    }

    /**
     * 删除自动化案例检擦点
     * @param id
     * @return
     */
    public Integer deleteAutoTestCaseCheck(Integer id) {

        AutoTestCaseCheckExample example = new AutoTestCaseCheckExample();
        AutoTestCaseCheckExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        return this.getMapper(AutoTestCaseCheckMapper.class).deleteByExample(example);
    }

    /**
     * 保存脚本信息
     * @param autoTestCaseParamsCode
     * @return
     */
    public Integer saveCode(AutoTestCaseParamsCode autoTestCaseParamsCode) {

        return this.getMapper(AutoTestCaseParamsCodeMapper.class).insert(autoTestCaseParamsCode);
    }

    /**
     * 保存sql脚本
     * @param autoTestCaseParamsSql
     * @return
     */
    public Integer saveSqlCode(AutoTestCaseParamsSql autoTestCaseParamsSql) {
        return this.getMapper(AutoTestCaseParamsSqlMapper.class).insert(autoTestCaseParamsSql);
    }

    /**
     * 删除sql脚本
     * @param paramId
     * @return
     */
    public Integer deleteSqlCode(Integer paramId) {

        return this.getMapper(AutoTestCaseParamsSqlMapper.class).deleteByPrimaryKey(paramId);
    }

    /**
     * 删除code脚本
     * @param paramId
     * @return
     */
    public Integer delteCode(Integer paramId) {
        return this.getMapper(AutoTestCaseParamsCodeMapper.class).deleteByPrimaryKey(paramId);
    }

    /**
     * 查询脚本（用于修改时回显）
     * @param paramId
     * @return
     */
    public AutoTestCaseParamsCode queryCode(Integer paramId) {
        return this.getMapper(AutoTestCaseParamsCodeMapper.class).selectByPrimaryKey(paramId);
    }

    /**
     * 查询sql脚本（用于修改时回显）
     * @param paramId
     * @return
     */
    public AutoTestCaseParamsSql querySqlCode(Integer paramId) {
        return this.getMapper(AutoTestCaseParamsSqlMapper.class).selectByPrimaryKey(paramId);
    }
    /**
     * 修改sql脚本
     * @param autoTestCaseParamsSql
     * @return
     */
    public Integer updateSqlCode(AutoTestCaseParamsSql autoTestCaseParamsSql) {
        return this.getMapper(AutoTestCaseParamsSqlMapper.class).updateByPrimaryKeySelective(autoTestCaseParamsSql);
    }

    /**
     * 修改脚本信息
     * @param autoTestCaseParamsCode
     * @return
     */
    public Integer updateCode(AutoTestCaseParamsCode autoTestCaseParamsCode) {

        return this.getMapper(AutoTestCaseParamsCodeMapper.class).updateByPrimaryKeySelective(autoTestCaseParamsCode);
    }

    /**
     * 根据sql模板ID删除sql
     * @param sqlTemplateId
     * @return
     */
    public Integer deleteSqlTemplateById(Integer sqlTemplateId) {

        return this.getMapper(AutoCollectTestCaseSqlMapper.class).deleteByPrimaryKey(sqlTemplateId);
    }

    /**
     * 更新sql模板。sql模板也就是自动录制的sql语句
     * @param autoCollectTestCaseSql
     * @return
     */
    public Integer updateSqlTemplate(AutoCollectTestCaseSql autoCollectTestCaseSql) {
        return this.getMapper(AutoCollectTestCaseSqlMapper.class).updateByPrimaryKeySelective(autoCollectTestCaseSql);
    }

    /**
     * 新增sql模板
     * @param autoCollectTestCaseSql
     * @return
     */
    public Integer addSqlTemplate(AutoCollectTestCaseSql autoCollectTestCaseSql) {

        // 获取当前表中最大的traceId, 什么是traceId？ traceId是录制sql时，用于在线程中标记同一线程。这样获取到的sql就能知道sql的真实执行顺序。
        KnowLedgeBaseBusiness knowLedgeBaseBusiness = new KnowLedgeBaseBusiness();
        knowLedgeBaseBusiness.setMapper(helper);
        List<AutoCollectTestCaseSql> allSqlByBusiCode = knowLedgeBaseBusiness.getAllSqlByBusiCode(autoCollectTestCaseSql.getBusiCode());
        if (allSqlByBusiCode != null && allSqlByBusiCode.size() > 0) {
            autoCollectTestCaseSql.setExt2(allSqlByBusiCode.get(0).getExt2());
        }
        return this.getMapper(AutoCollectTestCaseSqlMapper.class).insertSelective(autoCollectTestCaseSql);
    }

    public Integer addCheckSql(AutoTestCaseCheckSql autoTestCaseCheckSql) {

        return this.getMapper(AutoTestCaseCheckSqlMapper.class).insertSelective(autoTestCaseCheckSql);
    }

    /**
     * 批量添加校验sql
     * @param autoTestCaseCheckSqls
     * @return
     */
    public Integer addCheckSqls(List<AutoTestCaseCheckSql> autoTestCaseCheckSqls) {
        return this.getMapper(AutoTestCaseCheckSqlMapper.class).batchInsert(autoTestCaseCheckSqls);
    }

    /**
     * 查询关联sql
     * @param testCaseId
     * @return
     */
    public List<AutoTestCaseCheckSql> findChcekSqlsByTestCaseId(Integer testCaseId) {
        return this.getMapper(AutoTestCaseCheckSqlMapper.class).selectByTestCaseId(String.valueOf(testCaseId));
    }

    public int updateChcekSql(AutoTestCaseCheckSql autoTestCaseCheckSql) {
        return this.getMapper(AutoTestCaseCheckSqlMapper.class).updateByPrimaryKeySelective(autoTestCaseCheckSql);
    }

    public Integer deleteCheckSqlById(Integer id) {
        return this.getMapper(AutoTestCaseCheckSqlMapper.class).deleteByPrimaryKey(id);
    }

    public Integer updateRegularExpr(AutoTestCaseParamsRegular autoTestCaseParamsRegular) {
        return this.getMapper(AutoTestCaseParamsRegularMapper.class).updateByPrimaryKeySelective(autoTestCaseParamsRegular);
    }
}
